package com.et.machine

import android.content.Context
import androidx.lifecycle.ViewModel
import com.et.drive.info.IMachineIdInfoImpl
import com.et.drive.info.IMachineSignInfoImpl
import com.et.drive.info.MachineIdInfo
import com.et.drive.info.MachineInfo
import com.et.machine.convert.machineIdInfo
import com.et.machine.convert.machineInfo
import com.et.machine.store.DataBaseUtil
import com.tencent.mmkv.MMKV

class MainViewModel : ViewModel() {

    fun init(context: Context) {
        DataBaseUtil.init(context)
        MMKV.initialize(context)
        val kv = MMKV.defaultMMKV()
        if (machineInfo == null) {
            MachineInfo(
                kv.decodeString("version") ?: "0",
                kv.decodeString("uuid") ?: "0",
                kv.decodeString("type") ?: "0",
                kv.decodeInt("cDrink"),
                kv.decodeInt("cFood"),
                kv.decodeInt("cBox"),
                kv.decodeInt("nDrink"),
                kv.decodeInt("nFood"),
                kv.decodeInt("nBox")
            )

        }

        if (machineIdInfo == null) {
            MachineIdInfo(
                kv.decodeString("MCCMIdentificationCode") ?: "0",
                kv.decodeInt("nDrink2"),
                kv.decodeInt("nFood2"),
                kv.decodeInt("nFood3"),
                kv.decodeInt("nFood4")
            )

        }
        IMachineSignInfoImpl.observer {
            kv.encode("version", it.version)
            kv.encode("uuid", it.uuid)
            kv.encode("type", it.type)
            kv.encode("cDrink", it.cDrink)
            kv.encode("cFood", it.cFood)
            kv.encode("cBox", it.cBox)
            kv.encode("nDrink", it.nDrink)
            kv.encode("nFood", it.nFood)
            kv.encode("nBox", it.nBox)
        }
        IMachineIdInfoImpl.observer {
            kv.encode("MCCMIdentificationCode", it.MCCMIdentificationCode)
            kv.encode("nDrink2", it.nDrink2)
            kv.encode("nFood2", it.nFood2)
            kv.encode("nFood3", it.nFood3)
            kv.encode("nFood4", it.nFood4)
        }

    }

}